package com.bulemobi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.bulemobi.activity.Ac_CarService.Ac_DriverList;
import com.bulemobi.bean.AiDaiJiaBean;
import com.bulemobi.data.DataCentre;
import com.bulemobi.net.tsz.afinal.FinalBitmap;
import com.jsyh.hcb.R;

import java.util.ArrayList;

/**
 * Created by 武 on 2014/7/21 0021.
 * 司机列表adapter
 */
public class DriverAdapter extends BaseAdapter {
    private Context mContext;
    private FinalBitmap fb;
    private LayoutInflater layoutInflater;
    private ArrayList<AiDaiJiaBean> driverList;
    private String type;
    private String imei;

    public DriverAdapter(Context context, ArrayList<AiDaiJiaBean> list, String type, String imei) {
        this.mContext = context;
        this.driverList = list;
        this.type = type;
        this.imei = imei;
        fb = FinalBitmap.create(context.getApplicationContext());
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return driverList == null ? 0 : driverList.size();
    }

    @Override
    public Object getItem(int position) {
        return driverList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView tv_driverName;
        ImageView img_pic;
        RatingBar rb_rating;
        TextView tv_cishu = null;
        TextView tv_year;
        TextView tv_jiguan;
        TextView tv_status;
        TextView tv_distance;
        TextView tv_yuYue;
        if (null == mContext) {
            return null;
        }
        if (null == convertView) {
            convertView = layoutInflater.from(mContext).inflate(R.layout.ac_driver_item, null);
        }
        if (type.equals(DataCentre.AC_USERCENTRE)) {
            convertView.findViewById(R.id.lay_commen).setVisibility(View.GONE);
        }
        tv_driverName = (TextView) convertView.findViewById(R.id.tv_driverName);
        img_pic = (ImageView) convertView.findViewById(R.id.img_pic);
        rb_rating = (RatingBar) convertView.findViewById(R.id.rb_rating);
        tv_cishu = (TextView) convertView.findViewById(R.id.tv_cishu);
        tv_distance = (TextView) convertView.findViewById(R.id.tv_distance);
        tv_jiguan = (TextView) convertView.findViewById(R.id.tv_jiguan);
        tv_status = (TextView) convertView.findViewById(R.id.tv_status);
        tv_year = (TextView) convertView.findViewById(R.id.tv_year);
        tv_yuYue = (TextView) convertView.findViewById(R.id.tv_yuYue);
        fb.display(img_pic, driverList.get(position).getPic());
        tv_driverName.setText(driverList.get(position).getName());
        if (Integer.parseInt(driverList.get(position).getCishu()) < 29) {
            rb_rating.setRating(1);
        } else if (Integer.parseInt(driverList.get(position).getCishu()) > 29 && Integer.parseInt(driverList.get(position).getCishu()) < 59) {
            rb_rating.setRating(2);
        } else if (Integer.parseInt(driverList.get(position).getCishu()) > 59 && Integer.parseInt(driverList.get(position).getCishu()) < 89) {
            rb_rating.setRating(3);
        } else if (Integer.parseInt(driverList.get(position).getCishu()) > 89 && Integer.parseInt(driverList.get(position).getCishu()) < 119) {
            rb_rating.setRating(4);
        } else if (Integer.parseInt(driverList.get(position).getCishu()) > 119) {
            rb_rating.setRating(5);
        }
        tv_cishu.setText(driverList.get(position).getCishu());
        tv_distance.setText(driverList.get(position).getJuli());
        tv_jiguan.setText(driverList.get(position).getJiguan());
        tv_year.setText(driverList.get(position).getJialin());
        tv_status.setText(driverList.get(position).getState());
        if (type.equals("driver")) {
            tv_yuYue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        } else {
            tv_yuYue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent((mContext), Ac_DriverList.class);
                    intent.putExtra("index", position + "");
                    mContext.startActivity(intent);
                }
            });
        }
        return convertView;
    }


}
